package com.example.interviewrecorder.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.interviewrecorder.databinding.RecordItemBinding
import com.example.interviewrecorder.model.Record
import com.example.interviewrecorder.view.RecordDetailActivity
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class RecordAdapter(val recordList: List<Record>):RecyclerView.Adapter<RecordAdapter.ViewHolder>() {
    companion object{
        const val RECORD_KEY="record_data"
    }
    inner class ViewHolder(binding: RecordItemBinding):RecyclerView.ViewHolder(binding.root){
        val container=binding.container
        val titleTv=binding.titleTv
        val itvDateTv=binding.itvDateTv
        val durationTv=binding.durationTv
        val contentTv=binding.contentTv
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecordAdapter.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding=RecordItemBinding.inflate(inflater,parent,false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: RecordAdapter.ViewHolder, position: Int) {
        val record=recordList[position]
        holder.titleTv.text=record.title


        val date= Date(record.itvDate)
        val dateFormat=SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(date)
        holder.itvDateTv.text=dateFormat
        holder.durationTv.text=record.duration
        val content=record.content
        //内容过长，截取显示
        holder.contentTv.text=if (content.length>50) "${content.substring(0,50)}..." else content
        holder.container.setOnClickListener {
            RecordDetailActivity.start(record)
        }
    }

    override fun getItemCount(): Int {
        return recordList.size
    }
}